<!DOCTYPE html>
<html lang="en">

	<head>

		<meta charset="utf-8" />

		<title></title>

		<!-- IE -->
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<!-- 设置 viewport -->
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 兼容国产浏览器的高速模式 -->
		<meta name="renderer" content="webkit">
		<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->

		<!-- BEGIN 全局样式 -->
		<link rel="stylesheet" type="text/css" href="../../css/reset.css" />
		<link href="../../lib/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet" type="text/css" />
		<link rel="stylesheet" type="text/css" href="../../css/common.css" />

		<link rel="stylesheet" type="text/css" href="../../lib/bootstrapvalidator/dist/css/bootstrapValidator.css" />
		<!-- END 全局样式 -->
		<style type="text/css">

		</style>
	</head>

	<!-- END HEAD -->

	<!-- BEGIN BODY -->

	<body class="">
		<div class="container-fluid mt-15">
			<form class="form" id="form">
				<div class="form-group">
					<label for="name">学校名称</label>
					<input type="text" class="form-control" id="name" placeholder="请输入学校名称" name="name">
				</div>
				<div class="form-group">
					<label for="domain">域名</label>
					<input type="text" class="form-control" id="domain" placeholder="请输入域名" name="domain">
				</div>
				<div class="form-group fileBox">
					<label for="logo">上传logo</label>
					<input type="file" name="logo" id="logo" value="" />
				</div>
				<div class="form-group fileBox">
					<label for="image">上传背景图</label>
					<input type="file" name="image" id="image" value="" />
				</div>
				<div class="form-group">
					<label for="username">超级管理员账户</label>
					<input type="text" class="form-control" id="username" placeholder="请输入超级管理员账号" name="username">
				</div>

				<div class="form-group pwdBox">
					<label for="password">超级管理员密码</label>
					<input type="text" class="form-control" id="password" placeholder="请输入超级管理员密码" name="password">
				</div>
				
				<button type="button" class="btn btn-success btn-save">
			  	<span class="glyphicon glyphicon-floppy-saved"></span>
			  	保存
			  </button>
			</form>
		</div>

		<!--核心脚本sss-->
		<script src="../../lib/jquery/1.9.1/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../lib/layer/2.4/layer.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../lib/laypage/1.2/laypage.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../lib/bootstrap-3.3.7-dist/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>

		<script src="../../js/common/jsrsasign-all-min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/common/crypto-js.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/common/jquery.myPlugIn_v1.0.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/common/localstorage-ie.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../lib/bootstrapvalidator/dist/js/bootstrapValidator.js" type="text/javascript" charset="utf-8"></script>

		<!--[if lt IE 9]>
		<script src="../../js/excanvas.min.js"></script>
		<script src="../../js/respond.min.js"></script>  
		<![endif]-->
		<!--核心脚本eee-->

		<script type="text/javascript">
			$(document).ready(function() {
				if(localStorage.getItem("optionType") == "add") {
					$(".fileBox").remove();
				} else {
					$("#domain").attr({
//						"readonly": "readonly"
					});
					fnAjax.method_4_admin(
						url_join("admin/school/show"), {
							id: localStorage.getItem("nId")
						},
						"post",
						function(data) {
							console.log(data);
							$("#domain").val(data.data.school.domain);
							$("#name").val(data.data.school.name);
							$("#username").val(data.data.super.username);
							$("#password").removeAttr("name");
							$(".pwdBox").hide();
						}
					);
				}

				//初始化表单验证
				$('#form')
					.bootstrapValidator({
						message: '这个值是无效的',
						feedbackIcons: {
							valid: 'glyphicon glyphicon-ok',
							invalid: 'glyphicon glyphicon-remove',
							validating: 'glyphicon glyphicon-refresh'
						},
						fields: {
							name: {
								message: '名称是无效的',
								validators: {
									notEmpty: {
										message: '名称是必需的，不能是空的'
									},
									stringLength: {
										min: 2,
										max: 20,
										message: '名称必须大于2，小于20个字符'
									},
								}
							},

							domain: {
								validators: {
									notEmpty: {
										message: '域名是必需的，不能是空的'
									},

									regexp: {
										regexp: /^(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/,
										message: '请输入正确的域名'
									},

								}
							},
							logo: {
								validators: {
									//				                    notEmpty: {
									//				                        message: '上传图片不能为空'
									//				                    },
									file: {
										extension: 'png,jpg,jpeg',
										type: 'image/png,image/jpg,image/jpeg',
										maxSize: 1024 * 1024,
										message: '请重新选择logo图片,最大1m'
									}
								}
							},
							image: {
								validators: {
									//				                    notEmpty: {
									//				                        message: '上传图片不能为空'
									//				                    },
									file: {
										extension: 'png,jpg,jpeg',
										type: 'image/png,image/jpg,image/jpeg',
										maxSize: 5 * 1024 * 1024,
										message: '请重新选择背景图片,最大5m'
									}
								}
							},
							username: {
								message: '账号是无效的',
								validators: {
									notEmpty: {
										message: '账号是必需的，不能是空的'
									},
									stringLength: {
										min: 3,
										max: 20,
										message: '账号必须大于3，小于20个字符'
									},
									regexp: {
										regexp: /^[a-zA-Z0-9_\.]+$/,
										message: '账号只能由字母、数字、点和下划线组成'
									},
//									different: {
//										field: 'password',
//										message: '账号和密码不可能是相同的'
//									}
								}
							},
							
							password: {
								validators: {
									notEmpty: {
										message: '密码是必需的，不能是空的'
									},	
									stringLength: {
										min: 3,
										max: 20,
										message: '密码必须大于3，小于20个字符'
									},
									regexp: {
										regexp: /^[a-zA-Z0-9_\.]+$/,
										message: '密码只能由字母、数字、点和下划线组成'
									},
//									different: {
//										field: 'username',
//										message: '密码不可能与账号相同'
//									}
								}
							},
						}
					})
					.on('success.form.bv', function(e) {
						// 阻止表单提交
						e.preventDefault();
						//添加
						if(localStorage.getItem("optionType") == "add") {
							// 获得表单实例
							var $form = $(e.target);
							fnAjax.method_4_admin(
								url_join("admin/school/create"),
								//								$form.serialize(),
								$.serializeToObj($form.serialize()),
								"post",
								function(data) {
									console.log(data);
									layer.msg("操作成功", {
										time: 1500
									}, function() {
										parent.location.reload();
									});
								}
							);
						}
						//更新
						else {
							//不上传图片，便不传给后台
							if($("#image").val() == "") $("#image").parents(".form-group").remove();
							if($("#logo").val() == "") $("#logo").parents(".form-group").remove();
							$('#form').append($('<input type="hidden" value="' + localStorage.getItem("nId") + '" name="id"/>'));
							$('#form').append($('<input type="hidden" value="0" name="_school_id"/>'));
							var formData = new FormData($("#form")[0]); //form
							fnAjax.method_6(
								url_join("admin/school/update"),
								formData,
								"post",
								function(data) {
									console.log(data);
									layer.msg("操作成功", {
										time: 1500
									}, function() {
										parent.location.reload();
									});
								}
							);
						}

					});
				// 手动验证表单
				$('.btn-save').click(function() {
					$('#form').bootstrapValidator('validate');
				});

			});
		</script>
	</body>

</html>